package com.vgooo.client.myvg;

import java.net.URLEncoder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.UserInfo;
import com.vgooo.client.Main;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.VGApplication;
import com.vgooo.client.ViewLayout;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.JsonParserUtils;
import com.vgooo.util.MatchUtils;
import com.vgooo.util.Utils;

public class RegisterActivity extends VGActivity {

	private EditText nickName;
	private EditText eMail;
	private EditText pwd1;
	private EditText pwd2;
	private View register;

	private String nickname;
	private String email;
	private String password;
	private String token;
	private String result = "";
	private int type;

	private boolean success = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();
		addListener();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		Utils.hideSoftInput(pwd2);
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		nickName = (EditText) findViewById(R.id.register_user_nick_name);
		eMail = (EditText) findViewById(R.id.register_user_email);
		pwd1 = (EditText) findViewById(R.id.register_user_pwd1);
		pwd2 = (EditText) findViewById(R.id.register_user_pwd2);
		register = findViewById(R.id.register_ze);
	}

	@Override
	public void setupView() {
		// TODO Auto-generated method stub
		if (success) {
			success = false;
			ViewLayout.viwStack.pop();
			ViewLayout.setViewBack();
			Main.registerIntent.putExtra("type", 0);
			Main.registerIntent.putExtra("token", "");
		} else {
			Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initRegiterInfo();
			}
		});
	}

	@Override
	public void getData() {
		// TODO Auto-generated method stub
		String jsonString = "";
		String urlPath = HttpUtils.BASE_URI + "register" + "?email=" + email
				+ "&nickname=" + URLEncoder.encode(nickname) + "&psw="
				+ password + "&token=" + token + "&type=" + type;
		try {
			jsonString = HttpUtils.getJsonString(urlPath);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// throw new RuntimeException(e);
		}

		if (null == jsonString || "".equals(jsonString)) {
			success = false;
			result = "网络异常";
			return;
		}

		System.out.println(jsonString);
		try {
			UserInfo userInfo = JsonParserUtils.getUserInfo(jsonString);
			userInfo.isLogin = true;
			userInfo.password = password;
			switch (type) {
			case OtherLogin.TYPE_QQ:
				userInfo.QQToken = token;
				break;
			case OtherLogin.TYPE_WEI_BO:
				userInfo.weiBoToken = token;
				break;

			default:
				break;
			}
			VGApplication.userInfo = userInfo;
			success = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result  = new Gson().fromJson(jsonString, new TypeToken<String>() {
			}.getType());
			success = false;
			System.out.println(result);
			e.printStackTrace();
//			UmengUtils.onError(
//					this,
//					"解析" + urlPath
//							+ "地址的JSon数据出错(RegisterActivity.java :124) ："
//							+ e.toString());
//			success = false;
//			result = "服务器错误！";
		}

	}

	@Override
	public void doClick(View v) {
		// TODO Auto-generated method stub

	}

	private void initRegiterInfo() {
		nickname = nickName.getText().toString().trim();
		if (null == nickName || "".equals(nickname)) {
			nickName.setError("请输入昵称!");
			return;
		}

		email = eMail.getText().toString().trim();
		if (null == email || "".equals(email)) {
			eMail.setError("请输入邮箱！");
			return;
		}
		if (!MatchUtils.isEmail(email)) {
			eMail.requestFocus();
			eMail.setError("邮箱格式错误");
			return;
		}

		password = pwd1.getText().toString().trim();
		if (null == password || "".equals(password)) {
			pwd1.setError("请设置密码！");
			return;
		}
		if (password.length() < 6) {
			pwd1.setError("密码不能小于六位！");
			return;
		}
		String password2 = pwd2.getText().toString().trim();
		if (null == password2 || "".equals(password2)) {
			pwd1.setError("请再次输入密码！");
			return;
		}
		if (!password2.equals(password)) {
			pwd2.setError("两次输入的密码不一致！");
			return;
		}
		Intent intent = getIntent();
		token = intent.getStringExtra("token");
		type = intent.getIntExtra("type", 0);
		Utils.hideSoftInput(pwd2);
		init();
	}

}
